import json
import requests
from function_tool import function_help
from Test_lap import encode_and_decode


class Login(object):

    def login(self, username, password):
        user = encode_and_decode.AESUtils().encrypt(username)
        passwd = encode_and_decode.AESUtils().encrypt(password)
        headers = {'Content-Type': 'application/json'}
        auth_login = function_help.Function_Help().get_yaml_info()['info']['url'] + '/admin-api/system/auth/login'
        data = {"tenantName": "", "username": user, "password": passwd, "rememberMe": True}
        response_auth_login = requests.post(url=auth_login, data=json.dumps(data), headers=headers)
        if json.loads(response_auth_login.text)['code'] == 0:
            token = json.loads(response_auth_login.text)['data']['accessToken']
            headers['Authorization'] = "Bearer " + token
        else:
            raise Exception(username + '登录失败')
        return headers


if __name__ == '__main__':
    test = Login()
    result = test.login(username='admin', password='admin123')
    print(result)
